﻿package app.view.components.sections.expo 
{
	import app.view.components.guiDima.slideShowD.Image;
	import app.view.components.sections.LabeledSectionBase;
	import app.view.events.UIEvent;
	import flash.display.Sprite;
	import flash.events.MouseEvent;
	import gs.TweenLite;
	
	/**
	 * ...
	 * @author Dmitryi_Koruagin, icq:585466579, dmitryikoruagin@yandex.ru
	 */
	public class PopupExpoFiveExpositionDBase extends LabeledSectionBase
	{
		// app.view.components.sections.expo.PopupExpoFiveExpositionDBase
		
		//	public
		public var slider:Sprite;
		public var bgdImage_1:Sprite;
		public var bgdImage_2:Sprite;
		public var bgdImage_3:Sprite;
		public var bgdImage_4:Sprite;
		public var bgdImage_5:Sprite;
		
		private var _slider:Image;
		private var _image1:Image;
		private var _image2:Image;
		private var _image3:Image;
		private var _image4:Image;
		private var _image5:Image;
		private var _imageArray:Array;
		
		public var closeBtn:Sprite;
		public var bgd:Sprite;
		public var left_Btn:*;
		public var right_Btn:*;
		//	private
		private var _currentId:int;
		private var _isLast:Boolean;
		private var _isNext:Boolean;
		//	const
		private static const TIME_SHOW_NAVIGATE:Number = 0.3;
		private static const TIME_HIDE_NAVIGATE:Number = 0.2;
		
		//	CONSTRUCTOR
		public function PopupExpoFiveExpositionDBase() 
		{
			super();
		}
		
		override protected function init():void
		{
			super.init();
			
			_isLast = false;
			_isNext = false;
			left_Btn.alpha = 0;
			right_Btn.alpha = 0;
			left_Btn.selected = true;
			right_Btn.selected = true;
			
			// для области справа (тупо лень делать по другому)
			_slider = new Image(slider.width, slider.height, Image.RESIZE_MAX, Image.ALIGN_CENTER);
			addChild(_slider);
			_slider.x = slider.x;
			_slider.y = slider.y;
			//addChild(slider);
			
			_image1 = new Image(bgdImage_1.width, bgdImage_1.height, Image.RESIZE_MAX, Image.ALIGN_CENTER);
			addChild(_image1);
			_image1.x = bgdImage_1.x;
			_image1.y = bgdImage_1.y;
			//addChild(bgdImage_1);
			
			_image2 = new Image(bgdImage_2.width, bgdImage_2.height, Image.RESIZE_MAX, Image.ALIGN_CENTER);
			addChild(_image2);
			_image2.x = bgdImage_2.x;
			_image2.y = bgdImage_2.y;
			//addChild(bgdImage_2);
			
			_image3 = new Image(bgdImage_3.width, bgdImage_3.height, Image.RESIZE_MAX, Image.ALIGN_CENTER);
			addChild(_image3);
			_image3.x = bgdImage_3.x;
			_image3.y = bgdImage_3.y;
			//addChild(bgdImage_3);
			
			_image4 = new Image(bgdImage_4.width, bgdImage_4.height, Image.RESIZE_MAX, Image.ALIGN_CENTER);
			addChild(_image4);
			_image4.x = bgdImage_4.x;
			_image4.y = bgdImage_4.y;
			//addChild(bgdImage_4);
			
			_image5 = new Image(bgdImage_5.width, bgdImage_5.height, Image.RESIZE_MAX, Image.ALIGN_CENTER);
			addChild(_image5);
			_image5.x = bgdImage_5.x;
			_image5.y = bgdImage_5.y;
			//addChild(bgdImage_5);
		}
		
		private function setSelectedImage(ind:int=1):void
		{
			_currentId = ind;
			
			switch (ind) 
			{
				case 0:
					
					_image1.buttonMode = _image1.useHandCursor = false;
					_image2.buttonMode = _image2.useHandCursor = true;
					_image3.buttonMode = _image3.useHandCursor = true;
					_image4.buttonMode = _image4.useHandCursor = true;
					_image5.buttonMode = _image5.useHandCursor = true;
					_image1.removeEventListener(MouseEvent.CLICK, onCLickImage1);
					_image2.addEventListener(MouseEvent.CLICK, onCLickImage2);
					_image3.addEventListener(MouseEvent.CLICK, onCLickImage3);
					_image4.addEventListener(MouseEvent.CLICK, onCLickImage4);
					_image5.addEventListener(MouseEvent.CLICK, onCLickImage5);
					_slider.loadImage(_imageArray[0]);
				break;
				
				case 1:
					_image1.buttonMode = _image1.useHandCursor = true;
					_image2.buttonMode = _image2.useHandCursor = false;
					_image3.buttonMode = _image3.useHandCursor = true;
					_image4.buttonMode = _image4.useHandCursor = true;
					_image5.buttonMode = _image5.useHandCursor = true;
					_image1.addEventListener(MouseEvent.CLICK, onCLickImage1);
					_image2.removeEventListener(MouseEvent.CLICK, onCLickImage2);
					_image3.addEventListener(MouseEvent.CLICK, onCLickImage3);
					_image4.addEventListener(MouseEvent.CLICK, onCLickImage4);
					_image5.addEventListener(MouseEvent.CLICK, onCLickImage5);
					_slider.loadImage(_imageArray[1]);
				break;
				
				case 2:
					_image1.buttonMode = _image1.useHandCursor = true;
					_image2.buttonMode = _image2.useHandCursor = true;
					_image3.buttonMode = _image3.useHandCursor = false;
					_image4.buttonMode = _image4.useHandCursor = true;
					_image5.buttonMode = _image5.useHandCursor = true;
					_image1.addEventListener(MouseEvent.CLICK, onCLickImage1);
					_image2.addEventListener(MouseEvent.CLICK, onCLickImage2);
					_image3.removeEventListener(MouseEvent.CLICK, onCLickImage3);
					_image4.addEventListener(MouseEvent.CLICK, onCLickImage4);
					_image5.addEventListener(MouseEvent.CLICK, onCLickImage5);
					_slider.loadImage(_imageArray[2]);
				break;
				
				case 3:
					_image1.buttonMode = _image1.useHandCursor = true;
					_image2.buttonMode = _image2.useHandCursor = true;
					_image3.buttonMode = _image3.useHandCursor = true;
					_image4.buttonMode = _image4.useHandCursor = false;
					_image5.buttonMode = _image5.useHandCursor = true;
					_image1.addEventListener(MouseEvent.CLICK, onCLickImage1);
					_image2.addEventListener(MouseEvent.CLICK, onCLickImage2);
					_image3.addEventListener(MouseEvent.CLICK, onCLickImage3);
					_image4.removeEventListener(MouseEvent.CLICK, onCLickImage4);
					_image5.addEventListener(MouseEvent.CLICK, onCLickImage5);
					_slider.loadImage(_imageArray[3]);
				break;
				
				case 4:
					_image1.buttonMode = _image1.useHandCursor = true;
					_image2.buttonMode = _image2.useHandCursor = true;
					_image3.buttonMode = _image3.useHandCursor = true;
					_image4.buttonMode = _image4.useHandCursor = true;
					_image5.buttonMode = _image5.useHandCursor = false;
					_image1.addEventListener(MouseEvent.CLICK, onCLickImage1);
					_image2.addEventListener(MouseEvent.CLICK, onCLickImage2);
					_image3.addEventListener(MouseEvent.CLICK, onCLickImage3);
					_image4.addEventListener(MouseEvent.CLICK, onCLickImage4);
					_image5.removeEventListener(MouseEvent.CLICK, onCLickImage5);
					_slider.loadImage(_imageArray[4]);
				break;
			}
			
			checkedNavigation();
		}
		
		private function setEvents():void
		{
			closeBtn.addEventListener(MouseEvent.CLICK, onClosePopup);
			bgd.addEventListener(MouseEvent.CLICK, onClosePopup);
			//left_Btn.addEventListener(MouseEvent.CLICK, onClickLeftBtn);
			//right_Btn.addEventListener(MouseEvent.CLICK, onClickRightBtn);
			
			closeBtn.buttonMode = closeBtn.useHandCursor = true;
			//left_Btn.buttonMode = left_Btn.useHandCursor = true;
			//right_Btn.buttonMode = right_Btn.useHandCursor = true;
			
		}
		
		
		private function removeEvents():void
		{
			closeBtn.buttonMode = closeBtn.useHandCursor = false;
			closeBtn.removeEventListener(MouseEvent.CLICK, onClosePopup);
			bgd.removeEventListener(MouseEvent.CLICK, onClosePopup);
			//left_Btn.removeEventListener(MouseEvent.CLICK, onClickLeftBtn);
			//right_Btn.removeEventListener(MouseEvent.CLICK, onClickRightBtn);
			
		}
		
		override public function onDestroy():void 
		{
			// чистим что тут есть и вызываем супера
			removeEvents();
			
			_slider.destroy();
			removeChild(_slider);
			_image1.destroy();
			removeChild(_image1);
			_image2.destroy();
			removeChild(_image2);
			_image3.destroy();
			removeChild(_image3);
			_image4.destroy();
			removeChild(_image4);
			_image5.destroy();
			removeChild(_image5);
			
			super.onDestroy();
		};
		
		override public function get data():Object { return _data; };
		
		override public function set data(value:Object):void 
		{
			super.data = value;
			
			/*trace('currentId', _data.currentId);
			trace('data!!!+++---', _data.data[0]);
			trace('data!!!+++---', _data.data[1]);
			trace('data!!!+++---', _data.data[2]);*/
			
			//setParamHead(HeadMarkerBase.BGD_GREEN);
			
			_currentId=_data.currentId-1
			
			_imageArray = new Array();
			_imageArray = _data.data;
			
			_image1.loadImage(_imageArray[0]);
			_image2.loadImage(_imageArray[1]);
			_image3.loadImage(_imageArray[2]);
			_image4.loadImage(_imageArray[3]);
			_image5.loadImage(_imageArray[4]);
			
			setSelectedImage(_currentId);
			
			
			
			setEvents();
		};
		
		private function checkedNavigation():void
		{
			// проверяем навигацию
			// проверка
			////trace('checkedNavigation---+++',_currentId);
			if (_currentId>=1) 
			{
				TweenLite.to(left_Btn, TIME_HIDE_NAVIGATE, { alpha:1 } );
				left_Btn.addEventListener(MouseEvent.CLICK, onClickLeftBtn);
				_isLast = true;
				left_Btn.selected = !_isLast;
			}else
			{
				left_Btn.removeEventListener(MouseEvent.CLICK, onClickLeftBtn);
				_isLast = false;
				left_Btn.selected = !_isLast;
				TweenLite.to(left_Btn, TIME_HIDE_NAVIGATE, { alpha:0 } );
			}
			
			if (_currentId<_imageArray.length-1) 
			{
				TweenLite.to(right_Btn, TIME_HIDE_NAVIGATE, { alpha:1 } );
				right_Btn.addEventListener(MouseEvent.CLICK, onClickRightBtn);
				_isNext = true;
				right_Btn.selected = !_isNext;
			}else
			{
				right_Btn.removeEventListener(MouseEvent.CLICK, onClickRightBtn);
				_isNext = false;
				right_Btn.selected = !_isNext;
				TweenLite.to(right_Btn, TIME_HIDE_NAVIGATE, { alpha:0 } );
			}
			
		}
		
		private function onClosePopup(e:MouseEvent):void 
		{
			//trace('onClosePopup');
			removeEvents();
			
			var ev:UIEvent = new UIEvent(UIEvent.POPUP_CLOSE);
			dispatchEvent(ev);
			
		}
		
		private function onCLickImage1(e:MouseEvent):void 
		{
			setSelectedImage(0);
		}
		
		private function onCLickImage2(e:MouseEvent):void 
		{
			setSelectedImage(1);
		}
		
		private function onCLickImage3(e:MouseEvent):void 
		{
			setSelectedImage(2);
		}
		
		private function onCLickImage4(e:MouseEvent):void 
		{
			setSelectedImage(3);
		}
		
		private function onCLickImage5(e:MouseEvent):void 
		{
			setSelectedImage(4);
		}
		
		/**
		 * предыдущее событие
		 * @param	e
		 */
		private function onClickLeftBtn(e:MouseEvent):void 
		{
			_currentId = _currentId - 1;
			setSelectedImage(_currentId);
			//checkedNavigation();
		}
		
		/**
		 * следущее события
		 * @param	e
		 */
		private function onClickRightBtn(e:MouseEvent):void 
		{
			_currentId = _currentId + 1;
			setSelectedImage(_currentId);
			//checkedNavigation();
		}
	}

}